import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { getWeekMonthQuarterYear } from '/@/utils';
import {duplicateCheckDelay} from "@/views/system/user/user.api";
import {list, list as warehouseList} from "@/views/warehouse/WmsWarehouses.api";
import {list as storageZones} from "@/views/warehouse/WmsStorageZones.api";
//列表数据
export const columns: BasicColumn[] = [
   {
    title: '库位编码',
    align:"center",
    dataIndex: 'locationCode'
   },
   {
    title: '库位类别',
    align:"center",
    dataIndex: 'locationCategory',
     customRender: ({text}) => {
       return render.renderDict(text, 'location_category');
     }
   },
   {
    title: '库位类型',
    align:"center",
    dataIndex: 'locationType_dictText'
   },
   {
    title: '状态',
    align:"center",
     dataIndex: 'status',
     customRender: ({text}) => {
       return render.renderDict(text, 'wms_status');
     }
   },
   // {
   //  title: '所属仓库',
   //  align:"center",
   //  dataIndex: 'warehouseId'
   // },
  {
    title: '所属仓库',
    align:"center",
    dataIndex: 'warehouse.warehouseName',
    customRender: ({ record }) => {
      if(record.warehouse){
        return record.warehouse.warehouseName
      }
    }
  },
   // {
   //  title: '所属库区',
   //  align:"center",
   //  dataIndex: 'zoneId'
   // },
  {
    title: '所属库区',
    align:"center",
    dataIndex: 'storageZones.zoneName',
    customRender: ({ record }) => {
      if(record.storageZones){
        return record.storageZones.zoneName
      }
    }
  },
   {
    title: '巷道',
    align:"center",
    dataIndex: 'locationAisle'
   },
   {
    title: '排',
    align:"center",
    dataIndex: 'locationLine'
   },
   {
    title: '列',
    align:"center",
    dataIndex: 'locationRank'
   },
   {
    title: '层',
    align:"center",
    dataIndex: 'locationLayer'
   },
   {
    title: '长',
    align:"center",
    dataIndex: 'locationLength'
   },
   {
    title: '宽',
    align:"center",
    dataIndex: 'locationWidth'
   },
   {
    title: '容积',
    align:"center",
    dataIndex: 'locationCapacity'
   },
   {
    title: '承重',
    align:"center",
    dataIndex: 'loadCapacity'
   },
   {
    title: '是否可售',
    align:"center",
    dataIndex: 'isSellable_dictText'
   },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  //所属仓库
   {
     label: '所属仓库',
     field: 'warehouseId',
     component: 'ApiSelect',
     componentProps: {
       api: warehouseList,
       resultField: 'records',
       labelField: 'warehouseName',
       valueField: 'id',
       placeholder: '所属仓库',
     },
   }
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '库位编码',
    field: 'locationCode',
    component: 'Input',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入库位编码!'},
          ];
     },
  },
  {
    label: '库位类别',
    field: 'locationCategory',
    component: 'JDictSelectTag',
    componentProps:{
      dictCode:"location_category"
    },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入库位类别!'},
          ];
     },
  },
  {
    label: '库位类型',
    field: 'locationType',
    component: 'JDictSelectTag',
    componentProps:{
        dictCode:"location_type"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入库位类型!'},
          ];
     },
  },
  {
    label: '状态',
    field: 'status',
    defaultValue: "CREATED",
    component: 'JDictSelectTag',
    componentProps:{
        dictCode:"wms_status"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入状态: 0-禁用, 1-启用!'},
          ];
     },
  },
  {
    label: '所属仓库',
    field: 'warehouseId',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: warehouseList,
        resultField: 'records',
        labelField: 'warehouseName',
        valueField: 'id',
        onChange: (e: any) => {
          formModel.zoneId = undefined;
          formModel.isStart='2'
          if (e === undefined) {
            const { updateSchema } = formActionType;
            updateSchema({
              field: 'zoneId',
              component: 'Select',
              componentProps: {
                options: [],
              },
            });
            return ;
          }else{
            //请求库区数据
            storageZones( {
              warehouseId: e
            }).then((res) => {
              //抽取库区数据
              const zones= res.records.map(item => ({
                value: item.id,
                label: item.zoneName
              }));
              // console.log(zones)
              const { updateSchema } = formActionType;
              updateSchema({
                field: 'zoneId',
                component: 'Select',
                componentProps: {
                  options: zones,
                },
              });
            });
          }

        },
      };
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请选择所属仓库!'},
      ];
    },
  },
  {
    field: 'zoneId',
    component: 'Select',
    label: '所属库区',
    componentProps: {
      options: [], // defalut []
      placeholder: '所属库区',
    },
  },
  // {
  //   label: '所属库区',
  //   field: 'zoneId',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: storageZones,
  //     resultField: 'records',
  //     labelField: 'zoneName',
  //     valueField: 'id',
  //     placeholder: '所属库区',
  //   },
  //   dynamicRules: ({model,schema}) => {
  //         return [
  //                { required: true, message: '请输入所属库区!'},
  //         ];
  //    },
  // },

  // {
  //   field: 'zoneId',
  //   component: 'JTreeSelect',
  //   label: '所属库区',
  //   // helpMessage: ['component模式'],
  //   componentProps: {
  //     // dict: 'sys_test,name,id',
  //     dict: 'wms_storage_zones_view,name,id',
  //     pidField: 'parent_id',
  //   },
  //   dynamicRules: ({values}) => {
  //     return [
  //       { required: true, message: '请选择所属库区!',
  //         validator: (_, value) => {
  //           //如果value以'A'开头说明是仓库不是库区
  //           if (value && value.startsWith('A')) {
  //             return Promise.reject(new Error('请选择所属库区'));
  //           } else {
  //             return Promise.resolve();
  //           }
  //         },
  //       },
  //
  //     ];
  //   },
  //   colProps: {
  //     span: 12,
  //   },
  // },
  {
    label: '巷道',
    field: 'locationAisle',
    component: 'Input',
  },
  {
    label: '排',
    field: 'locationLine',
    component: 'Input',
  },
  {
    label: '列',
    field: 'locationRank',
    component: 'Input',
  },
  {
    label: '层',
    field: 'locationLayer',
    component: 'Input',
  },
  {
    label: '长',
    field: 'locationLength',
    component: 'InputNumber',
  },
  {
    label: '宽',
    field: 'locationWidth',
    component: 'InputNumber',
  },
  {
    label: '容积',
    field: 'locationCapacity',
    component: 'InputNumber',
  },
  {
    label: '承重',
    field: 'loadCapacity',
    component: 'InputNumber',
  },
  {
    label: '是否可售',
    field: 'isSellable',
    component: 'JDictSelectTag',
    componentProps:{
        dictCode:"yn"
     },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入是否可售!'},
          ];
     },
  },
	// TODO 主键隐藏字段，目前写死为ID
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
  //表单初始标记
   {
     label: '',
     field: 'isStart',
     component: 'Input',
     show: false,
     defaultValue: '1'
   }


];

// 高级查询数据
export const superQuerySchema = {
  locationCode: {title: '库位编码',order: 0,view: 'text', type: 'string',},
  locationCategory: {title: '库位类别',order: 1,view: 'text', type: 'string',},
  locationType: {title: '库位类型',order: 2,view: 'list', type: 'string',dictCode: 'location_type',},
  status: {title: '状态: 0-禁用, 1-启用',order: 3,view: 'list', type: 'string',dictCode: 'wms_status',},
  warehouseId: {title: '所属仓库',order: 4,view: 'link_table', type: 'string',},
  zoneId: {title: '所属库区',order: 5,view: 'link_table', type: 'string',},
  locationAisle: {title: '巷道',order: 6,view: 'text', type: 'string',},
  locationLine: {title: '排',order: 7,view: 'text', type: 'string',},
  locationRank: {title: '列',order: 8,view: 'text', type: 'string',},
  locationLayer: {title: '层',order: 9,view: 'text', type: 'string',},
  locationLength: {title: '长',order: 10,view: 'number', type: 'number',},
  locationWidth: {title: '宽',order: 11,view: 'number', type: 'number',},
  locationCapacity: {title: '容积',order: 12,view: 'number', type: 'number',},
  loadCapacity: {title: '承重',order: 13,view: 'number', type: 'number',},
  isSellable: {title: '是否可售',order: 14,view: 'list', type: 'string',dictCode: 'yn',},
};

/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[]{
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
